import java.io.*;
import java.net.*;

public class Cliente implements Runnable{
    
    // Declaration section
    // clientClient: the client socket
    // os: the output stream
    // is: the input stream

    
    static Socket clientSocket = null;
    static PrintStream os = null;
    static String aenviar=null;
    static  String[] ordenCompleta=null;
    static DataInputStream is = null;
   static  String ruta = "\\";
    static BufferedReader inputLine = null;
    static boolean closed = false;
    
    public static void main(String[] args) {

    
    // The default port    
    
    int port_number=2222;
        String host="10.16.104.26";
   
    // Initialization section:
    // Try to open a socket on a given host and port
    // Try to open input and output streams
    try {
            clientSocket = new Socket(host, port_number);
            inputLine = new BufferedReader(new InputStreamReader(System.in));

            os = new PrintStream(clientSocket.getOutputStream());
            is = new DataInputStream(clientSocket.getInputStream());
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host "+host);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to the host "+host);
        }
    
    // If everything has been initialized then we want to write some data
    // to the socket we have opened a connection to on port port_number 
    
        if (clientSocket != null && os != null && is != null) {
            try {
        
        // Create a thread to read from the server
        
                new Thread(new Cliente()).start();
        
        while (!closed) {
        			aenviar=inputLine.readLine();
        			ordenCompleta = aenviar.split(" ");
        			if (ordenCompleta[0].equals("dirL")) {
                        try {
                            File directorio = new File(ruta);
                            File[] archivos = directorio.listFiles();
                            for (int i = 0; i < archivos.length; i++) {
                                os.println(archivos[i].toString());
                            }
                        } catch (Exception ex) {
                            os.println("La ruta especificada no existe");
                        }
                    } else if (ordenCompleta[0].equals("IP")) {
                        test();
                    } else if (ordenCompleta[0].equals("rmL")) {
                        try {
                            File Aborrar = new File("c:\\" + ruta + "\\" + ordenCompleta[1]);
                            boolean success = Aborrar.delete();
                            if (!success) {
                                os.println("No se puede borrar");
                            }
                            if (success) {
                                os.println("Archivo borrado");
                            }
                        } catch (Exception ex) {
                            os.println("La ruta especificada no existe");
                        }
                    } else if (ordenCompleta[0].equals("cd")) {
                        if (ordenCompleta.length > 1) {
                            if (ordenCompleta[1].equals("..")) {
                                if (!ruta.equals("")) {
                                    ruta = ruta.substring(0, ruta.lastIndexOf("\\"));
                                    if (ruta.equals("")) {
                                        ruta = "\\";
                                    }
                                }
                            } else {
                                if (new File(ruta + ordenCompleta[1]).isDirectory()) {
                                    ruta += ordenCompleta[1];
                                } else if (new File(ruta + "\\" + ordenCompleta[1]).isDirectory()) {
                                    ruta += "\\" + ordenCompleta[1];
                                } else {
                                    os.println("La ruta especificada no existe");
                                }
                            }
                        }
                    } else if (ordenCompleta[0].equals("cpL")) {
                        if (ordenCompleta.length == 3) {
                            File archivoACopiar;
                            OutputStream out;
                            //Buscamos en el camino que fue proporcionado
                            if (new File(ordenCompleta[2]).isDirectory()) {
                                if (new File(ordenCompleta[1]).isFile()) {
                                    archivoACopiar = new File(ordenCompleta[1]);
                                    try (InputStream in = new FileInputStream(archivoACopiar)) {
                                        try {
                                            out = new FileOutputStream(ordenCompleta[2] + "/" + archivoACopiar.getName());
                                            byte[] buf = new byte[1024];
                                            int len;
                                            while ((len = in.read(buf)) > 0) {
                                                out.write(buf, 0, len);
                                            }
                                            in.close();
                                            out.close();
                                        } catch (Exception ex) {
                                            os.println(ex.getMessage());
                                        }
                                    } catch (Exception ex) {
                                        os.println(ex.getMessage());
                                    }
                                } //Buscamos en root
                                else if (new File(ruta + ordenCompleta[1]).isFile()) {
                                    archivoACopiar = new File("\\" + ordenCompleta[1]);
                                    try (InputStream in = new FileInputStream(archivoACopiar)) {
                                        try {
                                            out = new FileOutputStream(ordenCompleta[2] + "/" + archivoACopiar.getName());
                                            byte[] buf = new byte[1024];
                                            int len;
                                            while ((len = in.read(buf)) > 0) {
                                                out.write(buf, 0, len);
                                            }
                                            in.close();
                                            out.close();
                                        } catch (Exception ex) {
                                            os.println(ex.getMessage());
                                        }
                                    }
                                } //Buscamos en el directorio en el que estamos
                                else if (new File(ruta + "\\" + ordenCompleta[1]).isFile()) {
                                    archivoACopiar = new File(ruta + "\\" + ordenCompleta[1]);
                                    try (InputStream in = new FileInputStream(archivoACopiar)) {
                                        try {
                                            out = new FileOutputStream(ordenCompleta[2] + "/" + archivoACopiar.getName());
                                            byte[] buf = new byte[1024];
                                            int len;
                                            while ((len = in.read(buf)) > 0) {
                                                out.write(buf, 0, len);
                                            }
                                            in.close();
                                            out.close();
                                        } catch (Exception ex) {
                                            os.println(ex.getMessage());
                                        }
                                    }
                                } else {
                                    os.println("El archivo no existe o escribio la ruta de manera incorrecta");
                                }
                            } else {
                                os.println("El directorio destino no existe o escribio la ruta de manera incorrecta");
                            }
                        } else {
                            os.println("Comando incorrecto");
                        }
                    }else{
                    	os.println(aenviar); }
                }
        
        // Clean up:
        // close the output stream
        // close the input stream
        // close the socket
        
        os.close();
        is.close();
        clientSocket.close();   
            } catch (IOException e) {
                System.err.println("IOException:  " + e);
            }
        }
       
	}           
    
    public void run() {        
    String responseLine;
    
    // Keep on reading from the socket till we receive the "Bye" from the server,
    // once we received that then we want to break.
    try{ 
        while ((responseLine = is.readLine()) != null) {
            int i = 0;
            System.out.println(responseLine);
            if (responseLine.indexOf("*** Bye") != -1)
                break;
        }
            closed=true;
    } catch (IOException e) {
        System.err.println("IOException:  " + e);
    }
    }
    public static void test() {
    }
}